home *** CD-ROM | disk | FTP | other *** search
/ The Best of Down Under Games / The Best of Down Under Games.iso / 3dfx Screen Savers / VoodooLights / Sources / cell_util.c < prev    next >
C/C++ Source or Header  |  1997-07-23  |  4KB  |  167 lines

  1. /*------------------------------------------------------/
  2. /                                                        /
  3. /    Copyright 1997, SΘrgio Durte <smd@di.fct.unl.pt>    /
  4. /                                                        /
  5. /------------------------------------------------------*/
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <glide.h>
  12.  
  13. #include "defines.h"
  14. #include "mat.h"
  15. #include "rgb.h"
  16. #include "cell.h"
  17. #include "cell_util.h"
  18. #include "cell_tp1.h"
  19. #include "cell_tp2.h"
  20. #include "cell_tp3.h"
  21. #include "cell_tp4.h"
  22.  
  23. static Float Gravity = 1000.0 ;
  24.  
  25.  
  26. void cutl_random_pos( Float radius, Cell * c )
  27. {
  28.     c->pos.x = radius * ( 1.0 - 2.0 * rnd()) ;
  29.     c->pos.y = radius * ( 1.0 - 2.0 * rnd()) ;
  30.     c->pos.z = radius * ( 1.0 - 2.0 * rnd()) ;    
  31. }
  32.  
  33. void cutl_snap_pos_to_dad( Cell *c )
  34. {
  35.     Cell *dad = c->dad ;
  36.  
  37.     if( dad ) {
  38.         XYZ s ;
  39.         Float l ;
  40.         mat_subv( & c->pos, & dad->pos, & s ) ;
  41.         l = mat_direction( & s ) ;
  42.         if( l < ZERO ) mat_randomDir( & s ) ;
  43.         mat_combv( 0.5 * c->age, & s, & dad->pos, & c->pos ) ;
  44.   }
  45. }
  46.  
  47. void cutl_group_interaction(Cell *c, Cell **group, int n, Float scale, Float factor)
  48. {
  49.   int i ;
  50.   for( i = 0 ; i < n ; i++ ) {
  51.     Float a ;
  52.     XYZ s ;
  53.     Cell *q = group[i] ;
  54.     
  55.     if( q == c ) continue ;
  56.     
  57.     mat_subv( & q->pos, & c->pos, & s ) ;
  58.     a = mat_direction( & s ) ;
  59.     a = scale / (1.0 + pow( a, factor )) ;
  60.     if( fabs(a) > ZERO ) mat_combv( a, & s, & c->acc, & c->acc ) ;
  61.   }
  62. }
  63. void cutl_fgroup_interaction(Cell *c, Cell **group, int n, Float scale)
  64. {
  65.   int i ;
  66.   for( i = 0 ; i < n ; i++ ) {
  67.     Float l ;
  68.     XYZ s ;
  69.     Cell *q = group[i] ;
  70.     
  71.     if( q == c ) continue ;
  72.     
  73.     mat_subv( & q->pos, & c->pos, & s ) ;
  74.     l = mat_direction( & s ) ;
  75.     if( l > ZERO ) mat_combv( scale * l, & s, & c->acc, & c->acc ) ;
  76.   }
  77. }
  78.  
  79. void cutl_closest_interaction(Cell *c, Cell **group, int n, Float scale, Float factor)
  80. {
  81.   int i ;
  82.   Float mL = -1e8 ;
  83.   XYZ   mS ;
  84.  
  85.   for( i = 0 ; i < n ; i++ ) {
  86.     Float l ;
  87.     XYZ s ;
  88.     Cell *q = group[i] ;
  89.     
  90.     if( q == c ) continue ;
  91.     
  92.     mat_subv( & q->pos, & c->pos, & s ) ;
  93.     l = mat_direction( & s ) ;
  94.     l = 1.0 / ( 1 + pow( l, factor )) ;
  95.     if( l > mL ) {
  96.       mL = l ;
  97.       mS = s ;
  98.     }
  99.  
  100.   }
  101.   if( mL > ZERO ) mat_combv( scale * mL, & mS, & c->acc, & c->acc ) ;
  102. }
  103.  
  104. void cutl_indiv_interaction( Cell *c, Cell *q, Float factor) 
  105. {
  106.     Float l ;
  107.     XYZ s ;
  108.     mat_subv( & q->pos, & c->pos, & s ) ;
  109.     l = mat_direction( & s ) ;
  110.     if( l > ZERO ) mat_combv( factor * l, & s, & c->acc, & c->acc ) ;
  111. }
  112.  
  113. void cutl_stretch_to_age( Cell *c, Float scale )
  114. {
  115.   Float l ;
  116.   XYZ s ;
  117.   
  118.   return ;
  119.   if( c->dad ) {
  120.     mat_subv( & c->pos, & c->dad->pos, & s ) ;
  121.     l = mat_direction( & s ) ;
  122.     if( l < ZERO ) mat_randomDir( & s ) ;
  123.     
  124.     mat_combv( scale  * (c->age - l), & s, & c->acc, & c->acc ) ;
  125.   }
  126. }
  127.  
  128. void cutl_stretch_to_distance( Cell *c, Float distance, Float scale )
  129. {
  130.   Float l ;
  131.   XYZ s ;
  132.  
  133.   if( c->dad ) {
  134.     mat_subv( & c->pos, & c->dad->pos, & s ) ;
  135.     l = mat_direction( & s ) ;
  136.     if( l < ZERO ) mat_randomDir( & s ) ; 
  137.     mat_combv( scale  * (distance - l), & s, & c->acc, & c->acc ) ;
  138.   }
  139. }
  140.  
  141. void cutl_limit_bent( Cell *c, Float scale )
  142. {
  143.   Float l ;
  144.   XYZ s ;
  145.   Cell *dad, *gran ;
  146.  
  147.   dad = c->dad ;
  148.   if( ! dad ) return ;
  149.  
  150.   gran = dad->dad ;
  151.   if( ! gran ) return ;
  152.  
  153.   mat_subv( & c->pos, & gran->pos, & s ) ;  
  154.   l = mat_direction( & s ) ;
  155.   if( l < ZERO ) mat_randomDir( & s ) ; 
  156.   l = (Float)(1.0 / ( 1 + l )) ;
  157.   mat_combv( scale * l, & s, & c->acc, & c->acc ) ;
  158. }
  159.  
  160.  
  161. void cutl_limit_speed( Cell *c, Float maxV )
  162. {
  163.   Float v = mat_length( & c->vel ) ;
  164.   if( v > maxV ) mat_scalev( maxV / v, & c->vel, & c->vel ) ;
  165.  
  166. }
  167.